package com.hertz.alarm.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.hertz.alarm.controller.support.ResultModel;
import com.hertz.alarm.entity.IrisVersion;
import com.hertz.alarm.service.VersionService;

@Controller
@RequestMapping("/admin/pm")
public class VersionController extends BaseController{
	private Logger log = LoggerFactory.getLogger(VersionController.class);
	@Resource
	private VersionService versionService;
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ModelAndView list(HttpServletRequest request){
		List<IrisVersion> list = versionService.getAll();
		Map<String, Object> dataMap = new HashMap<String,Object>();
		dataMap.put("items", list);
		return returnModelAndView(dataMap, request);
	}
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody ResultModel<Object> save(
			HttpServletRequest request,
			@RequestParam(value="versionCode",required=true)Integer versionCode,
			@RequestParam(value="versionName",required=true)String versionName,
			@RequestParam(value="des",required=true)String des,
			@RequestParam(value="forced",required=false,defaultValue="false")Boolean forced,
			@RequestParam(value="downloadPath",required=true)String downloadPath){
		IrisVersion irisVersion = new IrisVersion();
		irisVersion.setVersionCode(versionCode);
		irisVersion.setVersionName(versionName);
		irisVersion.setForced(forced);
		irisVersion.setDes(des);
		irisVersion.setDownloadPath(downloadPath);
		irisVersion.setCtime(System.currentTimeMillis());
		versionService.save(irisVersion);
		return buildResultModel(Boolean.valueOf(true));
	}
	
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public @ResponseBody ResultModel<Object> get(
			HttpServletRequest request,
			@RequestParam(value="id",required=true)Integer id){
		IrisVersion irisVersion = versionService.getById(id);
		return buildResultModel(irisVersion);
	}
	
}
